//
//  ViewController.swift
//  Charts
//
//  Created by 红沙尘 on 2018/11/24.
//  Copyright © 2018年 红沙尘. All rights reserved.
//

import UIKit

class ViewController: UIViewController,ChartViewDelegate {


    var chartView: PieChartView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        chartView = PieChartView()
        chartView.frame = CGRect(x: 20, y: 80, width: self.view.bounds.width - 40,
                                 height: 260)
        chartView.drawHoleEnabled = false
        chartView.delegate = self
        self.view.addSubview(chartView)
        
        //生成5条随机数据
        let dataEntries = [PieChartDataEntry(value: 40,
                                             label: ""),
                           PieChartDataEntry(value: 60,
                                             label: "")]
//        let dataEntries = (0..<5).map { (i) -> PieChartDataEntry in
//            //ChartDataEntry
//            let entry = PieChartDataEntry(value: Double(arc4random_uniform(50) + 10),
//                                          label: "数据\(i)")
//
//            return entry
//        }

        let chartDataSet = PieChartDataSet(values: dataEntries, label: "数据分布")
        
        //设置颜色
        chartDataSet.colors = [NSUIColor(red: 248.0/255.0, green: 65.0/255.0, blue: 106.0/255.0, alpha: 1.0),
                               NSUIColor(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0, alpha: 1.0)]
//            ChartColorTemplates.vordiplom()
//            + ChartColorTemplates.joyful()
//            + ChartColorTemplates.colorful()
//            + ChartColorTemplates.liberty()
//            + ChartColorTemplates.pastel()
        let chartData = PieChartData(dataSet: chartDataSet)

        //设置饼状图数据
        chartView.data = chartData

        let entry = dataEntries[0] as ChartDataEntry

//        (dataEntries[0] as ChartDataEntry).
        chartView.highlightValue(Highlight(x: entry.centerX, y: entry.centerY, dataSetIndex: 0))
//        chartView.highlightValue(chartView.highlighted)
        
        
    }
    
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        print("xxxx=== ",highlight)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

